package com.ttx.pattern.example.proxy.jdk;

import com.ttx.pattern.example.proxy.after.RealSubject;
import com.ttx.pattern.example.proxy.after.RealSubject2;
import com.ttx.pattern.example.proxy.after.Subject;
import com.ttx.pattern.example.proxy.after.Subject2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * JDK动态代理
 *  <p>
 *      好处是写一个较为通用的"切面"类就可以了, 动态生成对应的代理类, 减少代码量
 *
 * @author TimFruit
 * @date 19-5-26 上午11:00
 */
public class Client {
    public static void main(String[] args) {

        RealSubject subject=new RealSubject();
        InvocationHandler proxyInvocationHandler=new ProxyInvocationHandler(subject);
        //动态生成代理类
        Subject subjectProxy= (Subject) Proxy.newProxyInstance(Client.class.getClassLoader(), new Class[]{Subject.class}, proxyInvocationHandler);
        subjectProxy.request();



        RealSubject2 subject2=new RealSubject2();
        InvocationHandler proxyInvocationHandler2=new ProxyInvocationHandler(subject2);
        // 动态生成代理类
        Subject2 subjectProxy2= (Subject2) Proxy.newProxyInstance(RealSubject2.class.getClassLoader(), new Class[]{Subject2.class}, proxyInvocationHandler2);
        subjectProxy2.request();
    }
}
